package org.hackystat.devcathlon.ui.page.matches.browse.matchpanel;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.ui.page.matches.browse.BrowseMatchDetailPage;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * The panel of on completed matches for the match browse page.
 * 
 * @author robin, scheller
 * 
 */
public class CompletedMatchPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Constructs the on going match panel.
   * 
   * @param id Component id.
   * @param listItem A list of matches.
   */
  public CompletedMatchPanel(String id, List<String> listItem) {
    super(id);
    List<String> listCurrentMatches = new ArrayList<String>();
    for (String matchName : listItem) {
      try {
        if (!(Tstamp.lessThan(Tstamp.makeTimestamp(), MatchManager.getInstance()
            .getMatch(matchName).getEndTime()))) {
          listCurrentMatches.add(matchName);
        }
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    String noCurrentMatches = "";
    if (listCurrentMatches.isEmpty()) {
      noCurrentMatches = "There are no completed matches";
    }
    add(new Label("noCurrentMatches", noCurrentMatches));
    DataView dataview = new DataView("ItemList", new ListDataProvider(listCurrentMatches)) {
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(Item item) {
        final String matchName = (String) item.getModelObject();
        item.add(new Label("MatchLabel", matchName));
        item.add(new Link("MatchLink") {
          private static final long serialVersionUID = 1L;

          /** Upon clicking this link, go to Match page */
          @Override
          public void onClick() {
            try {
              // setResponsePage(new MatchConfigurationPanel(mItem));
              setResponsePage(new BrowseMatchDetailPage(MatchManager.getInstance().getMatch(
                  matchName)));
              // setResponsePage(new );
            }
            catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        });
      }
    };
    add(dataview);
    // Add Labels for panel
    // add(new Label("currentMatches", "Current Matches"));
    add(new Label("completedMatches", "Completed Matches"));
    // add(new Label("lastCompleted", "Last 5 Completed Matches"));
  }
}
